//
//  ThirdViewController.m
//  BingoWaterfallFlowDemo
//
//  Created by giikylee on 16/4/19.
//  Copyright © 2016年 Bing. All rights reserved.
//

#import "ThirdViewController.h"
#import "rightTitleTableViewCell.h"
#import "DataTools.h"
#import "ThiredModel.h"
#import "XHLWaterLayout.h"
#import "FenleiCollectionViewCell.h"
#import "UIImageView+WebCache.h"
#import "TwoViewController.h"

#import "SouthJadeViewController.h"
#import "ProductModel.h"
#define ZXColor(r,g,b)  [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1];
#define   kScreen    CGRectGetWidth([UIScreen mainScreen].bounds)
@interface ThirdViewController ()<UITableViewDelegate,UITableViewDataSource,UICollectionViewDelegate,UICollectionViewDataSource,XHLWaterLayoutDelegate>
@property(nonatomic,strong)NSArray *nameProductArray;
@property(nonatomic,strong)NSMutableArray *array;
@property(nonatomic,strong)NSMutableArray *imageArray;
@property(nonatomic,strong)UICollectionView *collectionView;
//
@property(nonatomic,strong)NSString *urlStr;
@property(nonatomic,strong)NSMutableArray *dataArray;
//
@end

@implementation ThirdViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self creatView];
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"tabBarBg@2x.png"] forBarMetrics:UIBarMetricsDefault];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"消息副本"] style:UIBarButtonItemStyleDone target:self action:@selector(talkAction)];
    [self.navigationItem.rightBarButtonItem setTintColor:[UIColor whiteColor]];
    _nameProductArray = [NSArray arrayWithObjects:@"产品高端",@"整体分类",@"潮流品牌",@"潮流女装",@"时尚男装",@"酒水饮品",@"家具",@"电器",@"手机数码",@"电脑办公",@"奢侈礼品",@"汽车用品",@"玩具",@"时尚女装",@"产品高端",@"整体分类",@"潮流品牌",@"潮流女装",@"时尚男装",@"酒水饮品",@"家具",@"电器",@"手机数码",@"电脑办公",@"奢侈礼品",@"汽车用品",@"玩具",@"时尚女装", nil];
    [self getData];
    [self getProduct];
}
-(void)talkAction
{
    NSLog(@"我是聊天");
}
-(void)getData
{
    NSDictionary *dic = @{@"start":@"null", @"count":@"null"};
    NSString *productcategoryUrl = [ALLUrl stringByAppendingString:@"wap/productCategory/list"];
    [[DataTools shareDataTool] F_setHttpToJsonDict:productcategoryUrl params:dic type:@"F_POST" passValue:^(NSDictionary *dictionary) {
        self.dataArray = [NSMutableArray array];
        //NSLog(@"%@",[dictionary objectForKey:@"data"]);
        for (NSDictionary *d in [dictionary objectForKey:@"data"]) {
           ThiredModel *model = [[ThiredModel alloc]init];
            [model setValuesForKeysWithDictionary:d];
            [self.dataArray addObject:model];
            
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.rightTitleTableView reloadData];
                [self.rightTitleTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:YES scrollPosition:UITableViewScrollPositionTop];
            });
        }
        //NSLog(@"self.dataArry - %@",self.dataArray);
    } error:^(NSString *error) {
        NSLog(@"error:%@",error);
    }];
}
-(void)getProduct
{
    NSDictionary *dic = @{@"productCategoryId": @"1",@"start":@"1", @"count":@"10"};
    NSString *productcategoryUrl = [ALLUrl stringByAppendingString:@"wap/product/productCategoryList"];
    [[DataTools shareDataTool] F_setHttpToJsonDict:productcategoryUrl params:dic type:@"F_POST" passValue:^(NSDictionary *dictionary) {
        self.array = [[NSMutableArray alloc]init];
        self.imageArray = [[NSMutableArray alloc] init];
        for (NSDictionary *d in [dictionary objectForKey:@"data"]) {
            ProductModel *model = [[ProductModel alloc]init];
            [model setValuesForKeysWithDictionary:d];
            [self.array addObject:model];
            //NSLog(@"%@",model.name);
            if ([d objectForKey:@"image"]) {
                ProductModel *productModel = [[ProductModel alloc]init];
                [productModel setValuesForKeysWithDictionary:[d objectForKey:@"image"]];
                [self.imageArray addObject:productModel];
                NSLog(@"%@",productModel.url);
            }
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.collectionView reloadData];
        });
    } error:^(NSString *error) {
        NSLog(@"error:%@",error);
    }];
}
-(void)creatView
{
    self.kindSeacherBar = [[UISearchBar alloc]initWithFrame:CGRectMake(kScreen/15, 2,kScreen-kScreen/15*2 , 20)];
    self.kindSeacherBar.layer.cornerRadius = 10;
    self.kindSeacherBar.layer.masksToBounds = YES;
    [self.kindSeacherBar setBackgroundImage:[UIImage imageNamed:@"searchBackg.jpg"]];
    [self.kindSeacherBar.layer setBorderColor:[UIColor whiteColor].CGColor];
    self.navigationItem.titleView = self.kindSeacherBar;
    
    self.rightTitleTableView = [[UITableView alloc]initWithFrame:CGRectMake(kScreen-80, 0, 80, CGRectGetHeight([UIScreen mainScreen].bounds)-64-44) style:UITableViewStylePlain];
    self.rightTitleTableView.delegate = self;
    self.rightTitleTableView.dataSource = self;
    self.rightTitleTableView.bounces = NO;
    self.rightTitleTableView.showsVerticalScrollIndicator = NO;
    self.rightTitleTableView.showsHorizontalScrollIndicator = NO;
    self.rightTitleTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_rightTitleTableView];
    [self.rightTitleTableView registerNib:[UINib nibWithNibName:@"rightTitleTableViewCell" bundle:nil] forCellReuseIdentifier:@"rCell"];
    
    XHLWaterLayout *layout = [[XHLWaterLayout alloc]init];
    layout.delegate = self;
    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width-80, self.view.frame.size.height) collectionViewLayout:layout];
    //
    _collectionView.contentInset = UIEdgeInsetsMake(60, 0, 0, 0);
    UIView *guanggaoView = [[UIView alloc]initWithFrame:CGRectMake(0, -60, wKscreen-80, 60)];
    UIImageView *imageV = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"l_bg.jpg"]];
    imageV.frame = CGRectMake(0, 0, self.view.frame.size.width-80, 45);
    [guanggaoView addSubview:imageV];
    UILabel *lineFLbl = [[UILabel alloc]initWithFrame:CGRectMake(0, 55, (self.view.frame.size.width-80)/3-10, 1)];
    lineFLbl.backgroundColor = [UIColor grayColor];
    [guanggaoView addSubview:lineFLbl];
    UILabel *titleLbl = [[UILabel alloc]initWithFrame:CGRectMake((self.view.frame.size.width-80)/3, 52, (self.view.frame.size.width-80)/3, 8)];
    titleLbl.text = @"奢品男装";
    titleLbl.font = [UIFont systemFontOfSize:14];
    titleLbl.textAlignment = NSTextAlignmentCenter;
    [guanggaoView addSubview:titleLbl];
    UILabel *lineSLbl = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(titleLbl.frame)+10, 55, (self.view.frame.size.width-80)/3-10, 1)];
    lineSLbl.backgroundColor = [UIColor grayColor];
    [guanggaoView addSubview:lineSLbl];
    [_collectionView addSubview:guanggaoView];
    _collectionView.dataSource = self;
    _collectionView.delegate   = self;
    _collectionView.showsVerticalScrollIndicator = NO;
    _collectionView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_collectionView];
    [_collectionView registerNib:[UINib nibWithNibName:@"FenleiCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"cell"];
}
-(void)viewDidAppear:(BOOL)animated
{
    //[_rightTitleTableView selectRowAtIndexPath:[NSIndexPath indexPathWithIndex:1] animated:YES scrollPosition:UITableViewScrollPositionTop];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"rCell";
    rightTitleTableViewCell *rCell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
    if (rCell == nil) {
        rCell = [[rightTitleTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
    }
    
    ThiredModel *tModel = _dataArray[indexPath.row];
    rCell.nameProductLabel.text = tModel.name;
    //rCell.nameProductLabel.text = _nameProductArray[indexPath.row];
    rCell.backgroundColor = ZXColor(240, 240, 240);
    UIView *selectedBackgroundView = [[UIView alloc]initWithFrame:rCell.frame];
    selectedBackgroundView.backgroundColor = [UIColor whiteColor];
    rCell.selectedBackgroundView = selectedBackgroundView;
    //左侧示意条
    UIView *liner = [[UIView alloc]initWithFrame:CGRectMake(75, 0, 5, 30)];
    liner.backgroundColor = [UIColor redColor];
    [selectedBackgroundView addSubview:liner];
    return rCell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return  30;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.rightTitleTableView scrollToNearestSelectedRowAtScrollPosition:UITableViewScrollPositionTop animated:YES];
    [_collectionView scrollRectToVisible:CGRectMake(0, 0, self.collectionView.frame.size.width, self.collectionView.frame.size.height) animated:YES];
    _selectedIndex = indexPath.row;
    [_collectionView reloadData];
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.array.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    FenleiCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.nameLbl.font = [UIFont systemFontOfSize:8];
    ProductModel *model = self.array[indexPath.row];
    cell.nameLbl.text = model.name;
    ProductModel *imageModel = self.imageArray[indexPath.row];
    [cell.storeImage sd_setImageWithURL:[NSURL URLWithString:imageModel.url]];
    //ThiredModel *model = self.dataArray[indexPath.row];
    //[cell.storeImage sd_setImageWithURL:[NSURL URLWithString:model.model_img]];
    //根据右边点击的indepath更新左边内容;
    /*switch (_selectedIndex)
     {
     case 0:
     cell.storeImage.image = [UIImage imageNamed:@"3.jpg"];
     break;
     case 1:
     cell.storeImage.image = [UIImage imageNamed:@"2.jpg"];
     break;
     case 2:
     cell.storeImage.image = [UIImage imageNamed:@"3.jpg"];
     break;
     case 3:
     cell.storeImage.image = [UIImage imageNamed:@"2.jpg"];
     break;
     default:
     break;
     }*/
    
    return cell;
}

-(CGFloat)waterLayout:(UICollectionViewLayout *)waterLayout itemWidth:(CGFloat)itemWidth indexPath:(NSIndexPath *)indexPath
{
    ProductModel *imageModel = self.imageArray[indexPath.row];
    //NSLog(@"wwwwwww%@",self.imageArray[indexPath.row]);
    return  [imageModel.height floatValue]/[imageModel.width floatValue]*itemWidth;
//    return 80 + arc4random_uniform(100);
//    if (indexPath.row == 2) {
//        return itemWidth * 650 / 1198 + 21;
//    }
//   return itemWidth *600/800 + 60;
    
}

-(UIEdgeInsets)edgeInsetsInWaterflowLayout:(UICollectionViewLayout *)layout
{
    return UIEdgeInsetsMake(0, 2, 0, 0);
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    SouthJadeViewController *southVC = [[SouthJadeViewController alloc]init];
    [self.navigationController pushViewController:southVC animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
